setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top3domText <- read.csv("textpool/top3domText.csv", stringsAsFactors = FALSE)

### prepare tasks
LInarrowcareful <- validateLabel(type = "LI", n = 500, 
                                text.predict = top3domText, text.name = "post_text",
                                labels = c("Equal Pay for Women",
                                           "Healthcare/Reproductive Rights",
                                           "Agriculture",
                                           "Student Loan/Debt",
                                           "Drug Abuse",
                                           "Higher Education/Job Training",
                                           "Wall Street/Financial Sector",
                                           "Government Shutdown/Congressional Budget",
                                           "Obamacare/Tax Policy",
                                           "Deficits/Debt/Budget"), 
                                labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                                labels.add = NULL)

goldLI <- read.csv("goldstandard/goldLI.csv", stringsAsFactors = FALSE)
LInarrowcareful <- mixGold(tasks = LInarrowcareful, golds = goldLI)
LInarrowcareful <- recordTasks(type = "LI", tasks = LInarrowcareful,
                              path = "labelrecords/LInarrowcareful.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# LInarrowcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LInarrowcareful, HITidspath = "HITids/LInarrowcarefulids1.Rdata")
# ### extend HITs if needed
# for(i in LInarrowcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LInarrowcarefulids1.Rdata")
# LInarrowcarefulresults1 <- getResults(batch_id = "LInarrowcareful1", hit_ids = HITids, retry = FALSE)
# write.csv(LInarrowcarefulresults1, "LI/results/LInarrowcarefulresults1.csv", row.names = FALSE)
# evalResults(results = LInarrowcarefulresults1, key = LInarrowcareful, type = "LI")
# 
# ### send tasks (second trial)
# LInarrowcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "LI",
#                                tasksrecord = LInarrowcareful, HITidspath = "HITids/LInarrowcarefulids2.Rdata")
# ### extend HITs if needed
# for(i in LInarrowcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/LInarrowcarefulids2.Rdata")
# LInarrowcarefulresults2 <- getResults(batch_id = "LInarrowcareful2", hit_ids = HITids, retry = FALSE)
# write.csv(LInarrowcarefulresults2, "LI/results/LInarrowcarefulresults2.csv", row.names = FALSE)
# evalResults(results = LInarrowcarefulresults2, key = LInarrowcareful, type = "LI")